/**
 * Created by Administrator on 2017/3/2 0002.
 */
var Page1={
    create:function () {
    var page={};

        // var t0,t1,t2,t3,t4,b1,b2;
        var tw_whole,tw_t0,tw_t1,tw_t2,tw_t3,tw_t4,tw_b,tw_click,tw_bStart;
        page.ui=new PIXI.DisplayObjectContainer();

        var bg=new PIXI.Sprite(resource['assets/images/p1_bg.png'].texture);
        page.ui.addChild(bg);

        var t0=new PIXI.Sprite(resource['assets/images/p1_t0.png'].texture);
        t0.x=372;
        t0.y=175;
        t0.alpha=0;
        t0.anchor.set(0.5);
        page.ui.addChild(t0);

        var t1=new PIXI.Sprite(resource['assets/images/p1_t1.png'].texture);
        t1.x=367;
        t1.y=297;
        t1.alpha=0;
        t1.anchor.set(0.5);
        page.ui.addChild(t1);

        var t2=new PIXI.Sprite(resource['assets/images/p1_t2.png'].texture);
        t2.x=376;
        t2.y=436;
        t2.alpha=0;
        t2.anchor.set(0.5);
        page.ui.addChild(t2);

        var t3=new PIXI.Sprite(resource['assets/images/p1_t3.png'].texture);
        t3.x=376;
        t3.y=468;
        t3.alpha=0;
        t3.anchor.set(0.5);
        page.ui.addChild(t3);

        var t4=new PIXI.Sprite(resource['assets/images/p0_t1.png'].texture);
        t4.x=370;
        t4.y=60;
        t4.alpha=0;
        t4.anchor.set(0.5);
        page.ui.addChild(t4);

        var b0=new PIXI.Sprite(resource['assets/images/p1_build0.png'].texture);
        b0.x=63;
        b0.y=665;
        b0.alpha=1;
        page.ui.addChild(b0);

        var b1=new PIXI.Sprite(resource['assets/images/p1_build1.png'].texture);
        b1.x=63;
        b1.y=665;
        b1.alpha=0.8;
        page.ui.addChild(b1);
        var tw_b=new TWEEN.Tween({alpha:0.5})
            .to({alpha:1},1500)
            .onUpdate(function () {
                b1.alpha=this.alpha;
            })
            .delay(500)
            .repeat(Infinity)
            .yoyo(true)
            .easing(TWEEN.Easing.Cubic.InOut)
            .start();

        var tip= new PIXI.Sprite(resource['assets/images/tip0.png'].texture);
        tip.x = 370;
        tip.y = 1088;
        tip.alpha = 1;
        tip.anchor.set(0.5);
        // page.ui.addChild(tip);
        new TWEEN.Tween({y:1088,alpha:1})
            .to({y:1088+30,alpha:0.5},600)
            .onUpdate(function () {
                tip.y=this.y;
                tip.alpha=this.alpha;
            })
            .yoyo(true)
            .repeat(Infinity)
            .easing(TWEEN.Easing.Cubic.InOut)
            .start();

        var arr_texture=[];
        for(var i=1;i<25;i++)
        {
            if(i<10)
            {
                var texture = resource['assets/images/click000'+i.toString()+'.png'].texture;
            }
            else
            {
                var texture = resource['assets/images/click00'+i.toString()+'.png'].texture;
            }
            arr_texture.push(texture);

        }
        var sp_click=new PIXI.extras.AnimatedSprite(arr_texture);
        sp_click.anchor.set(0.5);
        sp_click.x=380;
        sp_click.y=900;
        sp_click.animationSpeed=0.4;
        sp_click.alpha=0;
        sp_click.gotoAndPlay(1);
        page.ui.addChild(sp_click);
        tw_click=new TWEEN.Tween({alpha:0,scale:0})
            .to({alpha:1,scale:1},1000)
            .onUpdate(function () {
                sp_click.scale.x=this.scale;
                sp_click.scale.y=this.scale;
                sp_click.alpha=this.alpha;
            })
            .delay(300)
            .easing(TWEEN.Easing.Back.Out);

        var click=new PIXI.Graphics();
        click.beginFill(0x0);
        click.drawRect(0,0,240,350);
        click.x=240;
        click.y=700;
        click.alpha=0.0;
        // click.interactive=true;
        page.ui.addChild(click);
        click.on('pointerdown',downHandler);
        click.on('pointerup',upHandler);

        var downPT={x:0,y:0};
        function upHandler(e) {
            var dis=(e.data.global.x-downPT.x)*(e.data.global.x-downPT.x)-(e.data.global.y-downPT.y)*(e.data.global.y-downPT.y)
            if(dis<25)
            {
                console.log('click');
                click.interactive=false;
                page.pageOut();
            }
        }
        function downHandler(e) {
            downPT.x=e.data.global.x;
            downPT.y=e.data.global.y;
        }

        page.In=function () {
            if(tw_whole)
            {
                tw_whole.stop();
                TWEEN.remove(tw_whole);
            }
            tw_bStart=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(100)
                .onUpdate(function () {
                    t0.alpha=this.alpha;
                    t0.scale.x=this.scale;
                    t0.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();

            tw_t0=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(100)
                .onUpdate(function () {
                    t0.alpha=this.alpha;
                    t0.scale.x=this.scale;
                    t0.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            tw_t1=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(300)
                .onUpdate(function () {
                    t1.alpha=this.alpha;
                    t1.scale.x=this.scale;
                    t1.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            tw_t2=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(500)
                .onUpdate(function () {
                    t2.alpha=this.alpha;
                    t2.scale.x=this.scale;
                    t2.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            tw_t3=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(700)
                .onUpdate(function () {
                    t3.alpha=this.alpha;
                    t3.scale.x=this.scale;
                    t3.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();
            tw_t4=new TWEEN.Tween({scale:0,alpha:0})
                .to({scale:1,alpha:1},1000)
                .delay(700)
                .onUpdate(function () {
                    t4.alpha=this.alpha;
                    t4.scale.x=this.scale;
                    t4.scale.y=this.scale;
                })
                .easing(TWEEN.Easing.Back.Out)
                .start();

            setTimeout(function () {
                tw_click.start();
                click.interactive=true;
            },2000)
        }
        page.pageOut=function ()
        {
            if(tw_click)
            {
                tw_click.stop()
            }
            sp_click.alpha=0;
            var event = new CustomEvent("Game_PageIn", {detail:2});
            document.dispatchEvent(event);
            if(tw_t0)
            {
                tw_t0.stop();
            }
            if(tw_t1)
            {
                tw_t1.stop();
            }
            if(tw_t2)
            {
                tw_t2.stop();
            }
            if(tw_t3)
            {
                tw_t3.stop();
            }
            if(tw_t4)
            {
                tw_t4.stop();
            }

            tw_whole=new TWEEN.Tween({alpha:1})
                .to({alpha:0},1000)
                .onUpdate(function () {
                    page.ui.alpha=this.alpha;
                })
                .onComplete(function () {
                    page.ui.alpha=1;
                    t0.alpha=0;
                    t1.alpha=0;
                    t2.alpha=0;
                    t3.alpha=0;
                    t4.alpha=0;
                    var event = new CustomEvent("Game_PageOut", {detail:1});
                    document.dispatchEvent(event);
                })
                .easing(TWEEN.Easing.Cubic.InOut)
                .start();
        }
        page.Out=function () {
            if(tw_t0)
            {
                tw_t0.stop();
            }
            if(tw_t1)
            {
                tw_t1.stop();
            }
            if(tw_t2)
            {
                tw_t2.stop();
            }
            if(tw_t3)
            {
                tw_t3.stop();
            }
            if(tw_t4)
            {
                tw_t4.stop();
            }

            tw_whole=new TWEEN.Tween({alpha:1})
                .to({alpha:0},400)
                .onUpdate(function () {
                    page.ui.alpha=this.alpha;
                })
                .onComplete(function () {
                    page.ui.alpha=1;
                    t0.alpha=0;
                    t1.alpha=0;
                    t2.alpha=0;
                    t3.alpha=0;
                    t4.alpha=0;
                })
                .easing(TWEEN.Easing.Cubic.InOut)
                .start();
        }

    return page;
    }
}